//
//  UIView+Gradient.swift
//  ShowDance
//
//  Created by 苏泊尔漫也要按时吃饭 on 2022/12/14.
//
// Usage:
// myButton.gradientColor(CGPoint(x: 0, y: 0.5), CGPoint(x: 1, y: 0.5), [UIColor(hex: "#FF2619").cgColor, UIColor(hex: "#FF8030").cgColor])

import UIKit

public extension UIView {
    
    // MARK: 添加渐变色图层
    func gradientColor(_ startPoint: CGPoint, _ endPoint: CGPoint, _ colors: [Any], _ locations: [NSNumber]) {
        
        guard startPoint.x >= 0, startPoint.x <= 1, startPoint.y >= 0, startPoint.y <= 1, endPoint.x >= 0, endPoint.x <= 1, endPoint.y >= 0, endPoint.y <= 1 else {
            return
        }
        
        // 外界如果改变了self的大小，需要先刷新
        layoutIfNeeded()
        
        var gradientLayer: CAGradientLayer!
        
        removeGradientLayer()
        
        gradientLayer = CAGradientLayer()
        gradientLayer.frame = self.bounds
        gradientLayer.startPoint = startPoint
        gradientLayer.endPoint = endPoint
        gradientLayer.locations = locations
        gradientLayer.colors = colors
        gradientLayer.cornerRadius = self.layer.cornerRadius
        gradientLayer.masksToBounds = true
        // 渐变图层插入到最底层，避免在uibutton上遮盖文字图片
        self.layer.insertSublayer(gradientLayer, at: 0)
        self.backgroundColor = UIColor.clear
        // self如果是UILabel，masksToBounds设为true会导致文字消失
        self.layer.masksToBounds = false
    }
    
    func gradientBorderColor(_ startPoint: CGPoint, _ endPoint: CGPoint, _ colors: [Any], _ locations: [NSNumber], _ radius: CGFloat, _ lineWidth: CGFloat) {
        guard startPoint.x >= 0, startPoint.x <= 1, startPoint.y >= 0, startPoint.y <= 1, endPoint.x >= 0, endPoint.x <= 1, endPoint.y >= 0, endPoint.y <= 1 else {
            return
        }
        
        // 外界如果改变了self的大小，需要先刷新
        layoutIfNeeded()
        
        var gradientLayer: CAGradientLayer!
        
        removeGradientLayer()
        
        gradientLayer = CAGradientLayer()
        gradientLayer.frame = self.bounds
        gradientLayer.startPoint = startPoint
        gradientLayer.endPoint = endPoint
        gradientLayer.locations = locations
        gradientLayer.colors = colors
        gradientLayer.cornerRadius = radius
        gradientLayer.masksToBounds = true
//        self.backgroundColor = UIColor.clear
        self.layer.insertSublayer(gradientLayer, at: 0)
//        self.layer.addSublayer(gradientLayer)
        self.layer.masksToBounds = false
        
        let shapeLayer = CAShapeLayer()
        shapeLayer.borderWidth = 1
        shapeLayer.path = UIBezierPath(roundedRect: gradientLayer.bounds, cornerRadius: radius).cgPath
        shapeLayer.fillColor = UIColor.clear.cgColor // 必须要设置成clearColor或者nil，默认是黑色
        shapeLayer.strokeColor = UIColor.white.cgColor // 随便设置一个非clearColor的颜色
                
        gradientLayer.mask = shapeLayer
    }
    
    // MARK: 移除渐变图层
    // （当希望只使用backgroundColor的颜色时，需要先移除之前加过的渐变图层）
    func removeGradientLayer() {
        if let sl = self.layer.sublayers {
            for layer in sl {
                if layer.isKind(of: CAGradientLayer.self) {
                    layer.removeFromSuperlayer()
                }
            }
        }
    }
    
    func gradientStyle3() {
        self.gradientColor(CGPoint(x: 0.34, y: 0.06), CGPoint(x: 0.56, y: 1), [UIColor(hexString: "#DEFFF2")?.cgColor as Any,UIColor(hexString: "#FFEFFB")?.cgColor as Any,UIColor(hexString: "#ECEDFF")?.cgColor as Any], [0,0.5,1.0])
    }
    
    func gradientStyle5() {
        self.gradientBorderColor(CGPoint(x: 0, y: 0), CGPoint(x: 1, y: 1), [UIColor(hexString: "#0BFF9F")?.cgColor as Any,UIColor(hexString: "#FF13C8")?.cgColor as Any,UIColor(hexString: "#1C18F4")?.cgColor as Any], [0,0.5,1.0], 8, 1)
    }
    
    func gradientStyle4() {
        self.gradientColor(CGPoint(x: 1, y: 0.67), CGPoint(x: 0.09, y: 0), [UIColor(hexString: "#0BFF9F")?.cgColor as Any,UIColor(hexString: "#FF13C8")?.cgColor as Any,UIColor(hexString: "#1C18F4")?.cgColor as Any], [0,0.5,1])
    }
    
    func gradientStyleWhite() {
        self.gradientColor(CGPoint(x: 1, y: 0.67), CGPoint(x: 0.09, y: 0), [UIColor.white], [0,0.5,1])
    }
    
    func gradientLevel1(){
        self.gradientColor(CGPoint(x: 0.5, y: 0.02), CGPoint(x: 0.5, y: 0.98), [UIColor(hexString: "#F2F0F0")?.cgColor as Any,UIColor(hexString: "#FFFFFF")?.cgColor as Any], [0,1.0])
    }
    
    func gradientLevel2(){
        self.gradientColor(CGPoint(x: 0.5, y: 0.02), CGPoint(x: 0.5, y: 0.98), [UIColor(hexString: "#DDFFEE")?.cgColor as Any,UIColor(hexString: "#FFFFFF")?.cgColor as Any], [0,1.0])
    }
    
    func gradientLevel3(){
        self.gradientColor(CGPoint(x: 0.5, y: 0.05), CGPoint(x: 0.5, y: 0.96), [UIColor(hexString: "#E2EFFF")?.cgColor as Any,UIColor(hexString: "#FFFFFF")?.cgColor as Any], [0,1.0])
    }
    
    func gradientLevel4(){
        self.gradientColor(CGPoint(x: 0.5, y: 0.31), CGPoint(x: 0.5, y: 0.91), [UIColor(hexString: "#EFE2FF")?.cgColor as Any,UIColor(hexString: "#FFFFFF")?.cgColor as Any], [0,1.0])
    }
    
    func gradientLevel5(){
        self.gradientColor(CGPoint(x: 0.34, y: 0.06), CGPoint(x: 0.56, y: 1), [UIColor(hexString: "#FFF3E1")?.cgColor as Any,UIColor(hexString: "#FFFFFF")?.cgColor as Any], [0,1.0])
    }
    
    func danceLevel4(){
        self.gradientColor(CGPoint(x: 0, y: 0.5), CGPoint(x: 0.92, y: 0.44), [UIColor(hexString: "#EFE2FF")?.cgColor as Any,UIColor(hexString: "#FFFFFF")?.cgColor as Any], [0,1.0])
    }
    
}
